package com.qj.sys.enums;

import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author dlw
 * @date 2025-06-04 11:58
 */
@Getter
public enum ChannelEnum {
    DOU_YIN(0, "抖音渠道"),
    BILIBILI(1, "B站渠道");

    private int code;
    private String desc;

    ChannelEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    // 将枚举放在Map中，用空间换时间(用于枚举中东西非常多)
    public static final Map<Integer, ChannelEnum> clientChannelEnum = Stream.of(ChannelEnum.values()).collect(Collectors.toMap(item -> item.getCode(), item -> item));

    //根据code值获取map中对应的desc
    public static String getValueByCodeAsMap(int codeVal) {
        ChannelEnum channelEnum = clientChannelEnum.get(codeVal);
        if (channelEnum == null) {
            return null;
        }
        return channelEnum.getDesc();
    }

    /**
     * 根据code值获取渠道枚举
     */
    public static ChannelEnum getByCode(int codeVal) {
        for (ChannelEnum channelEnum : ChannelEnum.values()) {
            if (channelEnum.code == codeVal) {
                return channelEnum;
            }
        }
        return null;
    }

    /**
     * 根据code值获取desc
     */
    public static String getValueByCode(int codeVal) {
        for (ChannelEnum channelEnum : ChannelEnum.values()) {
            if (channelEnum.code == codeVal) {
                return channelEnum.desc;
            }
        }
        return null;
    }
}
